# -*- coding: utf8 -*-
"""
Created on Tue Oct 16 16:31:24 2012

@author: (Ago) Milan Augustin

aaaugustin@gmail.com
"""

from PyQt4.QtCore import QSettings
import serial, time
#from PyQt4.Qt import QTimer

class PohybZ():
    """
    Ovládání osy (Z)
    """
    def __init__(self, portN):
        #self.com = serial.Serial(0, baudrate=38400, timeout = 1, xonxoff=True)  # inicializace portu COM1
        #self.com.write("Otevře port\n")
        #self.rx = self.com.readline()     # do promenne rx nacte vse co je ve vstupnim bufferu
        #print self.rx                # vypise nacteny retezec
        settings = QSettings("data.ini", QSettings.IniFormat)        
        self.sroubKoef = settings.value('sroubKoef', type = float)
        self.sroubKoefP = settings.value('sroubKoefP', type = float)
        self.sroubKoefPP = settings.value('sroubKoefPP', type = float)
        print "Sroub koeficient", self.sroubKoef
        print "Posuv koeficient", self.sroubKoefP
        print "Pomaly posuv koeficient", self.sroubKoefPP
        
        nP=int(self.sroubKoefPP*-3000)
        nP=str(nP)        
        self.nahoruPom = "!0V250\r !0A50\r !0L"+ nP +"\r"
        n=int(self.sroubKoefP*-15000)
        n=str(n)        
        self.nahor = "!0V5000\r !0A100\r !0L"+ n +"\r"
#        self.nahor = "!0V600\r !0A50\r !0L0,400,0\r"  # VPRAVO
        dP=int(self.sroubKoefPP*3000)
        dP=str(dP) 
        self.doluPom = "!0V250\r !0A50\r !0L"+ dP +"\r"
        d=int(self.sroubKoefP*15000)
        d=str(d)        
        self.dol = "!0V5000\r !0A100\r !0L"+ d +"\r"
#        self.dol = "!0V600\r !0A50\r !0L0,-400,0\r"  #VLEVO
        kN=int(self.sroubKoef*-1600)
        kN=str(kN)
        self.krokN = "!0V1800\r !0A20\r !0L"+ kN +"\r" #!0V1350\r !0A10\r !0L-2400\r
        self.pocetCV = 0
        
        self.port = False  
        if portN:
            self.port = True
        else:
            self.port = False
        self.cisteni = False        
        self.prvy = True
        
    
    def referenceZ(self):
        if self.port:
            self.otevreP()
            self.com.write("!0SR2\r")
            self.rx = self.com.readline() 
            self.com.write("!0L0,0,1<CR><LF>\r")
            print "Referencni pohyb Z", self.rx
            self.com.write("!0WX20000,3500,2000,500\r")
            time.sleep(5)
            self.com.write("!0L0,0,1<CR><LF>\r")
            #time.sleep(10)
            self.com.write("!0P\r")   # aktualni poloha
            self.rx = self.com.readline()
            print "Poloha aktualni =", self.rx
            #self.com.write("!0L0,0,1<CR><LF>\r")
            self.zavreP()
            self.setPolohaZNul()
            print " PRIPRAVENO K TISKU !!!"
        else:
            print " Reference nulová poloha"
            
  #-------------------------------------------------------------- ČIŠŤĚNÍ         
     # Zjistí aktuální polohu osy X       
    def getPolohaZ(self):
        if self.port:
            self.otevreP() 
            self.com.write("!0NY0\r !0P\r")   # aktualni poloha X vynuluje Y
            self.rx = self.com.readline()
            print "Poloha aktualni =", self.rx
            self.zavreP()
        else:
            print " Reference nulová poloha"   
    
    # Nastaví čítač (Gravos) polohy X na (0)        
    def setPolohaZNul(self):
        if self.port:
            self.otevreP()
            self.com.write("!0NX0\r")   # aktualni poloha
            self.ro = self.com.readline()
            print "Poloha aktualni nastavena =", self.ro
            self.zavreP()
        else:
            print " Reference nulová poloha"
              
      
      # Spínání rele A
    def onClona(self):
        if self.port:
            self.otevreP()
            self.com.write("!0L0,0,-1<CR><LF>\r")
            self.zavreP()
            print "Rele zapnuto - ON  clona"
        
        else:
            print "ON Clona"
            
    def offClona(self):
        """
        Zavře clonu
        """
        if self.port:
            self.otevreP()
            self.com.write("!0L0,0,1<CR><LF>\r")
            self.zavreP()
        else:  
            print "Rele vypnuto - OFF clona"            
            
      # Spínání rele B
    def onSvetloCisti(self):
        print "Svetlo (cisteni) zapnuto"
        if self.port:
            self.otevreP()
            self.com.write("!0V0,3,0\r !0L0,-50000,0\r")
            self.zavreP()
        else:
            print "Cistici Svetlo"
        time.sleep(3)    # doba expozice cisteni
        #return
       
       # Spínání rele C
    def onReleC(self):
        if self.port:
            self.otevreP() 
#            self.com.write("Rele C")
#            self.cti = self.com.readline()
#            print self.cti  
            self.com.setRTS(0)
            self.com.setDTR(1)
            print "Rele C SEPNUTO"
            time.sleep(10)

            
            self.zavreP()
            
        else:
            print "Sepne rele C"
            
       # Spínání rele D
    def onReleD(self):
        if self.port:
            self.otevreP() 
            #self.com.write("Rele D")
            #self.cti = self.com.readline()
            #print self.cti 
            self.com.setDTR(0)
            self.com.setRTS(1)
            print "Rele D SEPNUTO"
#            time.sleep(10)
#            self.com.setRTS(0)
#            print "Rele D vypnuto"
            time.sleep(10)
            
            self.zavreP()
            
        else:
            print "Sepne rele C"            

    def onSvetlo(self):
        if self.port:
            self.otevreP()
            self.com.write("!0L0,0,-1<CR><LF>\r")
            self.zavreP()
            print "Rele vypnuto"
        
        else:
            print "Cistici Svetlo"

    def ofSvetlo(self):
        if self.port:
            self.otevreP()
            self.com.write("!0L0,0,1<CR><LF>\r")
            self.zavreP()
        else:
            print "Cistici Svetlo"     
    
  #---------------------------------------------------------------------------
      
        
    def resetGravos(self):
        self.otevreP()
        self.com.write("!0i1\r")
        rx = self.com.readline() 
        print rx
        if rx =="0,FF\r":
            print "Horní Koncák"
            self.com.write("!0sw0,01\r !0sw2,00\r !0sw4,00\r")
            self.com.write("!0V1000\r !0A10\r !0L-1000\r")
            
        else:
            print "Bez pohybu"
        time.sleep(1) 
        self.com.write("!0sw0,01\r !0sw2,01\r !0sw4,01\r")
        self.zavreP()
        print "RESET"
       
       # Konfigurace ridici desky Gravos
    def onGravos(self):
        if self.port:
            self.otevreP()
            self.com.write("!0sw0,01\r !0sw2,00\r !0sw4,00\r")
            self.com.write("!0L0,0,1\r")
            self.rx = self.com.readline()
            print self.rx
            self.zavreP()
        else:
            print "Konfiguruje ridici desku Gravos"
        
    # Sepne rele(1) na desce Gravos (Zapne projektor)         
    def onProjektor(self):
        if self.port:
            self.otevreP()
            self.com.write("!0O0,FB\r")
            time.sleep(1)
            self.com.write("!0O0,FF\r")
            self.zavreP()
        else:
            print "Zapíná projektor"
            
    # Vypne projektor        
    def offProjektor(self):
        self.otevreP()                 
        self.com.write("!0O0,FB\r")           # zapne relé , Tlačítko projektor poprvé
        time.sleep(1)
        self.com.write("!0O0,FF\r")            # vypne  relé
        time.sleep(1)
        self.com.write("!0O0,FB\r")            # zapne relé , Tlačítko projektor podruhé
        time.sleep(1)
        self.com.write("!0O0,FF\r")           # vypne relé            
        self.zavreP()
        print  "Vypíná projektor"            
          
   # Jede nahoru osa Z
    def nahoru(self, pomalu):
        if self.port:
            self.otevreP()
            if pomalu:
                self.com.write(self.nahoruPom)
            else:
                self.com.write(self.nahor)
                #time.sleep(1)
                kn = self.com.readline()
                print kn
            self.zavreP()

        else:
            print "Jede nahoru pomalu nebo rychle"
         

    def dolu(self, pomalu):      
        if self.port:        
            self.otevreP() 
            if pomalu:
                self.com.write(self.doluPom)     
            else:
                self.com.write(self.dol)   
            self.d = self.com.readline()
            self.zavreP()
        else:
            print "Jede dolu pomalu nebo rychle"
            

    def stop(self):
        if self.port:
            self.otevreP()
            self.com.write("!0H\r")         # zastaví
            self.com.write("!0D\r")      # smaže frontu
            #self.s = self.com.readline()
            self.zavreP()
            self.getPolohaZ()
        else:
            print "STOP !"
        

    def krok(self, delka):
        if self.port:  
            if delka =="a":
                kDa=int(self.sroubKoef*1582)
                kDa=str(kDa)                
                self.krokD = "!0V7000\r !0A100\r !0L"+ kDa +"\r" 
            if delka =="b":
                # 1 puls = 2 mic
                kDb=int(self.sroubKoef*1572)
                kDb=str(kDb)                
                self.krokD = "!0V7000\r !0A100\r !0L"+ kDb +"\r" 
            if delka =="c":
                kDc=int(self.sroubKoef*1550)
                kDc=str(kDc)                 
                self.krokD = "!0V7000\r !0A100\r !0L"+ kDc +"\r" 
                
            self.otevreP()
            self.com.write("!0L0,0,-1<CR><LF>\r")
            self.com.write(self.krokN)
            self.kn = self.com.readline()
            self.com.write("!0L0,0,1<CR><LF>\r")
            time.sleep(0.3) # prodleva pro krok nahoru
            self.com.write(self.krokD)
            self.kd = self.com.readline()
            self.zavreP()

        else:
            print "Jeden krok - nahoru dolu (podle nastavení a, b nebo c)"
          
        
    def krokNah(self, mic, pocet):
        self.mi =mic
        if self.port:
            self.otevreP()
            if self.mi=="a":
                self.poXa = pocet*18
                self.poXa=int(self.sroubKoef*self.poXa)
                self.poXa = str(self.poXa)
                self.com.write("!0V3000\r !0A10\r !0L-"+self.poXa+"\r")    # X35 mic    
            if self.mi=="b":
                self.poXb = pocet*27
                self.poXb=int(self.poXb*self.sroubKoef)
                self.poXb = str(self.poXb)
                self.com.write("!0V3000\r !0A10\r !0L-"+self.poXb+"\r")   # X54 mic 
            if self.mi=="c":
                self.poXc = pocet*50
                self.poXc=int(self.poXc*self.sroubKoef)
                self.poXc = str(self.poXc)            
                self.com.write("!0V3000\r !0A10\r !0L-"+self.poXc+"\r")   # X90 mic
            self.zavreP()
            
        else:
            print " X kroků nahoru"
            
            
    def krokDol(self, mic, pocet):
        self.mi =mic
        if self.port:
            self.otevreP()
            if self.mi=="a":
                self.pXa = pocet*18
                self.pXa=int(self.pXa*self.sroubKoef)
                self.pXa = str(self.pXa)            
                self.com.write("!0V3000\r !0A10\r !0L"+self.pXa+"\r")    # -36X mic        
            if self.mi=="b":
                self.pXb = pocet*27
                self.pXb=int(self.pXb*self.sroubKoef)
                self.pXb = str(self.pXb)            
                self.com.write("!0V3000\r !0A10\r !0L"+self.pXb+"\r")   # -54X mic    
            if self.mi=="c":
                self.pXc = pocet*50
                self.pXc=int(self.pXc*self.sroubKoef)
                self.pXc = str(self.pXc)            
                self.com.write("!0V3000\r !0A10\r !0L"+self.pXc+"\r")   # -90X mic
            self.zavreP()
            
        else:
            print " X kroků dolu"
            
            
    def portNe(self):
        self.port = False

    def otevreP(self):
        if self.port:  #(port='COM1', baudrate=19200, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=0, rtscts=0
            self.com = serial.Serial(0, baudrate=38400, bytesize=8, parity='N', stopbits=1, timeout = 1, xonxoff=0, rtscts=0)  # inicializace portu COM1
            
        else:
            print "Otevírá port..Není zařízení"              
        
    def zavreP(self):
        if self.port: 
            self.com.close()             # uzavre port
        else:
            print "Zavírá port..Není zařízení" 
            





